/*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <sourcemod>
#include <sdktools>
#include <sdkhooks>

#include "macros.sp"
#include "helpers.sp"

new Handle:deagle_damage_hs = INVALID_HANDLE;
new String:headsounds[6][256];

public OnPluginStart()
{
	deagle_damage_hs = CreateConVar("l4d2_deagledamage_hs", "2000.0", "How much damage do the deagle in headshot", CVAR_FLAGS);

	headsounds[0] = "player/survivor/splat/zombie_blood_spray_01.wav";
	headsounds[1] = "player/survivor/splat/zombie_blood_spray_02.wav";
	headsounds[2] = "player/survivor/splat/zombie_blood_spray_03.wav";
	headsounds[3] = "player/survivor/splat/zombie_blood_spray_04.wav";
	headsounds[4] = "player/survivor/splat/zombie_blood_spray_05.wav";
	headsounds[5] = "player/survivor/splat/zombie_blood_spray_06.wav";

	HookEvent("player_hurt", event_player_hurt);
}

public OnMapStart()
{	
	PrecacheSound(headsounds[0]);
	PrecacheSound(headsounds[1]);
	PrecacheSound(headsounds[2]);
	PrecacheSound(headsounds[3]);
	PrecacheSound(headsounds[4]);
	PrecacheSound(headsounds[5]);
	
}
public event_player_hurt(Handle:event, const String:name[], bool:dontBroadcast)
{
	new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
	new target = GetClientOfUserId(GetEventInt(event, "userid"));
	
	if(IsValidSurvivorAlive(attacker) && IsValidInfected(target))
	{
		new hitgroup = GetEventInt(event, "hitgroup");
		if(hitgroup == 1) //headshot
		{
			decl String:weapon[64];
			GetEventString(event, "weapon", weapon, sizeof(weapon));
			if(strcmp(weapon, "pistol_magnum") == 0) //do a headshot
			{
				PrintToChat(attacker, "\x05Deagle Headshot on %N!", target);
				PrintToChat(target, "\x05Deagle Headshot by %N!", attacker);
				SDKHooks_TakeDamage(target, 0, attacker, GetConVarFloat(deagle_damage_hs), DMG_BULLET, 0);

				/*play sound on SI head */
				EmitSoundToAll(headsounds[GetRandomInt(0, 5)], target);
			}

		}
	}

}
